import { Injectable } from '@angular/core';
import { Http, Jsonp, URLSearchParams } from '@angular/http';
import { ServiceBase } from "./serviceBase";

@Injectable()
export class StoreService extends ServiceBase {
    constructor(protected http: Http, protected jsonp: Jsonp) { super(); }
    /**
     * @param  {} userId 店长的用户Id
     */
    async getStores(userId): Promise<Store[]> {
        let url = "api/users/" + userId + "/stores?page=0";
        let r: any = await this.GetCloud(url);
        let result: Store[] = [];

        for (let item of r) {
            if (item.logo) {
                item.logoDisplay = this.CloudHost + ":" + this.CloudPort + item.logo;
                //Ngnix代理之后使用相对API地址
                // item.logoDisplay = item.logo;
                result.push(item);
            }
            else {
                item.logoDisplay = this.CloudHost + ":" + this.CloudPort + item.merchant.logo;
                // item.logoDisplay = item.merchant.logo;
                result.push(item);
            }
        }

        return result;
    }

    /**
     * 获取店铺配置信息
     * @param keys 配置对应的key的数组 
     */
    async getStoreConfigs(key: string) {
        let store = this.CurrentStore;
        let config = null;
        if(!store){
            return null;
        }
        if (store) {
            if (store.configs) {
                for (let k = 0; k < (<any>store.configs).length; k++) {
                    if (store.configs[k].key == key) {
                        config = store.configs[k].value;
                        break;
                    }
                }
            }
        }
        return config;
    }

    async getStoreInfo(){
        let storeId = this.CurrentStore.id;
        if (!storeId) {
            return null;
        }
        let url = "api/system/storeInfo/" + storeId;
        let store = await this.Get(url);
        return store;
    }
}

interface Store {
    logoDisplay: string
}